<?php
namespace app\center\logic;

class LoginLogic extends BaseLogic {

    /**
     * 检查登录控制台规则
     * @param $openid
     * @param $code
     * @return mixed
     */
    public function check_login_admin_rule($openid,$code){
        if(empty($code)){
            return $this->get_response(0,'登录失败');
        }

        //检查二维码
        $maps = [];
        $maps[] = ['code','=',$code];
        $alc_detail = model('admin_login_code')->get_detail($maps);
        if(empty($alc_detail)){
            return $this->get_response(0,'登录失败');
        }
        $now_times = time();
        $c_times = strtotime($alc_detail['c_time']);
        if($now_times - $c_times >= 300){
            return $this->get_response(0,'该二维码已过期');
        }
        if(!empty($alc_detail['openid'])){
            return $this->get_response(0,'该二维码已使用');
        }

        //检查用户
        $maps = [];
        $maps[] = ['openid','=',$openid];
        $maps[] = ['role','in',[2,3]];
        $u_detail = model('user')->get_detail($maps,'id,enable');
        if(empty($u_detail)){
            return $this->get_response(0,'该用户不是管理员');
        }
        if($u_detail['enable'] == 0){
            return $this->get_response(0,'该用户已禁用');
        }

        $data['alc_detail'] = $alc_detail;
        return $this->get_response(1,'OK',$data);
    }
}

